package com.casic.ops.entity.vo;

import com.casic.ops.consts.app.ActionStatus;
import com.casic.ops.entity.domain.ApplicationReleaseMachineDO;
import com.casic.ops.utils.Utils;
import com.orion.utils.convert.TypeStore;
import lombok.Data;

import java.util.List;

/**
 * @ClassName ops-platform
 * @Description  应用发布统计机器
 * @Author mianbin
 * @Date 2022/5/31 10:59
 * @Version 1.0
 **/
@Data
public class ApplicationReleaseStatisticsMachineVO {

    /**
     * id
     */
    private Long id;

    /**
     * 机器id
     */
    private Long machineId;

    /**
     * 机器名称
     */
    private String machineName;

    /**
     * 状态 10未开始 20进行中 30已完成 40执行失败 50已跳过 60已取消
     *
     * @see ActionStatus
     */
    private Integer status;

    /**
     * 机器操作时长ms
     */
    private Long used;

    /**
     * 机器操作时长
     */
    private String usedInterval;

    /**
     * 发布操作
     */
    private List<ApplicationStatisticsActionLogVO> actionLogs;

    public static ApplicationReleaseStatisticsMachineVO releaseMachineDO2releaseStatisticsMachineVO(ApplicationReleaseMachineDO p) {
        ApplicationReleaseStatisticsMachineVO vo = new ApplicationReleaseStatisticsMachineVO();
        vo.setId(p.getId());
        vo.setMachineId(p.getMachineId());
        vo.setMachineName(p.getMachineName());
        vo.setStatus(p.getRunStatus());
        // 设置构建用时
        if (p.getStartTime() != null && p.getEndTime() != null) {
            long used = p.getEndTime().getTime() - p.getStartTime().getTime();
            vo.setUsed(used);
            vo.setUsedInterval(Utils.interval(used));
        }
        return vo;
    }

    static {
        TypeStore.STORE.register(ApplicationReleaseMachineDO.class, ApplicationReleaseStatisticsMachineVO.class, p -> {
            ApplicationReleaseStatisticsMachineVO vo = new ApplicationReleaseStatisticsMachineVO();
            vo.setId(p.getId());
            vo.setMachineId(p.getMachineId());
            vo.setMachineName(p.getMachineName());
            vo.setStatus(p.getRunStatus());
            // 设置构建用时
            if (p.getStartTime() != null && p.getEndTime() != null) {
                long used = p.getEndTime().getTime() - p.getStartTime().getTime();
                vo.setUsed(used);
                vo.setUsedInterval(Utils.interval(used));
            }
            return vo;
        });
    }
}
